package com.briup.estore.service.impl;

import com.briup.estore.common.pojo.Category;
import com.briup.estore.common.utils.BeanCopyUtils;
import com.briup.estore.common.vo.CategoryInfoVO;
import com.briup.estore.dao.CategoryDao;
import com.briup.estore.dao.impl.CategoryDaoImpl;
import com.briup.estore.service.CategoryService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午2:21
 * @Description
 **/

public class CategoryServiceImpl implements CategoryService {
    private CategoryDao categoryDao =new CategoryDaoImpl();


    @Override
    public List<CategoryInfoVO> getAllCategory( ) {
        ArrayList<CategoryInfoVO> list = new ArrayList<>();
//        查询一级分类
        List<Category> categories = categoryDao.selectFirstCategory();
        categories.forEach(i->{
            CategoryInfoVO vo = BeanCopyUtils.copyBean(i, CategoryInfoVO.class);
            List<Category> categoryList = categoryDao.selectByParentId(i.getId());
            List<CategoryInfoVO.SecondCategoryInfo> secondCategoryInfos = BeanCopyUtils.copyList(categoryList, CategoryInfoVO.SecondCategoryInfo.class);
            vo.setCategoryInfos(secondCategoryInfos);
            list.add(vo);
        });

        return list;
    }

    @Override
    public String getName(int id) {
        return categoryDao.findNameById(id);
    }
}
